(function() {
    angular.module('app').component('shopStatDataComponent', {
    
        templateUrl: 'app/components/shop.stat.data.component/shop.stat.data.component.html',
        controller: shopStatDataComponent,
        controllerAs: 'vm',
        bindings: {
            shopUid: '<'
        }
    });

    shopStatDataComponent.$inject = ['shopStatDataComponentService', 'util', '$rootScope'];

    function shopStatDataComponent(shopStatDataComponentService, util, $rootScope) {
        var vm = this;
        // vm.shopUid
        vm.shopInfo = {};



        // 店铺内不同类目的销售额对比
        vm.pieOption = {
            title: {
                text: '各类目销售额占比',
                subtext: '数据实时来自淘宝、天猫',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                // formatter: "{a} <br/>{b} : {c} ({d}%)"
                formatter: function(params) {
                    // console.log(params)
                    var str = '<div class="center-align">' + params.seriesName + '</div>';

                    str += '<div style = "text-align: left;">' + params.name + ' : ' + util.bigNumFormat(params.value) + ' (' + params.percent + '%)</div>';

                    return str;
                }
            },
            legend: {
                orient: 'vertical',
                left: 'right',
                data: []
            },
            series: [{
                name: '销售额',
                type: 'pie',
                radius: '55%',
                center: ['50%', '55%'],
                data: [],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };

        vm.lineOption = {
            title: {
                text: '店铺销售额走势',
                subtext: '数据实时更新自淘宝、天猫',
                x: 'center'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function(params) {
                    var str = '<div style = "text-align: left;">' + params[0].name + '</div>';

                    for (var i = 0; i < params.length; i++) {
                        str += '<div style = "text-align: left;padding-left:10px;">' + params[i].seriesName + ' : ' + util.bigNumFormat(params[i].data) + '</div>';
                    }

                    return str;
                }
            },
            grid: {
                x: 54,
                // left:'10%',             
                right: '10%',
                bottom: '10%'
            },
            legend: {
                data: [],
                x: 'right',
                y: 'center',
                orient: 'vertical'
            },
            xAxis: {
                data: [],
                axisLabel: {
                    //rotate: 50,
                    show: true
                }
            },
            yAxis: [{
                type: 'value',
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000).toFixed(0) + '万';
                            case 6:
                                return (value / 10000).toFixed(0) + '万';
                            case 7:
                                return (value / 10000).toFixed(0) + '万';
                            case 8:
                                return (value / 10000).toFixed(0) + '万';
                            case 9:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 10:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 11:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 12:
                                return (value / 100000000).toFixed(2) + '亿';
                        }

                        return value;
                    }
                }
            }],
            series: [{
                name: '销售额',
                type: 'line',
                data: []
            }, {
                name: '销售量',
                type: 'line',
                data: []
            }]
        };

        init();

        function init() {

            if($(window).width()<=992 && $(window).width()>600){
                $('.shopStatTableBox table.responsive-table').removeClass('responsive-table');
            }
            
            $rootScope.$emit('echart_draw_line_loading_EV', 'shopStatDataLine');
            shopStatDataComponentService.getShopInfo(vm.shopUid).then(function(data) {
                // vm.shopInfo = data;

                vm.shopInfo = util.shopLevelFormat(data);
                // console.log(vm.shopInfo);             

                var obj = util.historyFormat(data.historySellMoney);
                var obj2 = util.historyFormat(data.historySellAmount);

                var option = angular.copy(vm.lineOption);

                option.xAxis.data = obj.x;
                option.series[0].data = obj.y;
                option.series[1].data = obj2.y;

                $rootScope.$emit('echart_draw_line_EV', option, 'shopStatDataLine');

            }, function(err) {

            });


            $rootScope.$emit('echart_draw_line_loading_EV', 'shopStatDataPie');
            //获取拼图
            shopStatDataComponentService.getShopProductCatValue(vm.shopUid).then(function(data) {
                var option = angular.copy(vm.pieOption);

                for (var i in data.cat) {
                    option.series[0].data.push({
                        name: i,
                        value: data.cat[i]
                    });
                }

                $rootScope.$emit('echart_draw_line_EV', option, 'shopStatDataPie');
            }, function(err) {

            });
        }

    }
})();
